cap log close
clear all
set more off

global oregon_data "***/OHIE_QJE_Replication/Data"
global aca_penalty_data "ACA/Results/Data"
global results "*****/Results"

local date "052020"

log using "$results/Logs/oregon_analysis_`date'.log", replace

use "$aca_penalty_data/count_by_age_111219.dta", clear

reshape wide count, i(age_2017) j(notall16)
rename age_2017 age 
gen count = count0 + count1
egen total_count1 = sum(count1)
egen total_count = sum(count)
gen weights_aca_penalty_notall16 = 1/(count1/total_count1)
gen weights_aca_penalty = 1/(count/total_count)
keep age weights*

save "$aca_penalty_data/age_weights_`date'.dta", replace

use "$oregon_data/data_for_analysis.dta", clear

gen age = 2008 - birthyear_list
merge m:1 age using "$aca_penalty_data/age_weights_111219.dta"
drop if _merge != 3
drop _merge

gen ins_months_past_year = ins_months_6m + ins_months_12m
gen weight_6_12m = 1/((1/weight_12m)*(1/weight_6m))
xi, prefix(qqq) i.wave_survey6m*i.numhh_list


**** Column 1 

cap mat drop itt_acr_noweights

*ITT 20% Sample 
regress postn_death treatment nnn* lll* if ins_months_past_year != . [pw = weight_6_12m], cl(household_id)
mat itt_acr_noweights = nullmat(itt_acr_noweights) , _b[treatment]*100, _se[treatment]*100

*First Stage (months on any insurance from around July/August 2008 - July/August 2009 ), 20% sub-sample
reg ins_months_past_year treatment ddd*  qqq* [pw = weight_6_12m] , cl(household_id ) noomit
mat itt_acr_noweights = nullmat(itt_acr_noweights) , _b[treatment] , _se[treatment]


*ACR 20% Sample 
ivregress 2sls postn_death (ins_months_past_year = treatment ) nnn* lll* [pw = weight_6_12m], cl(household_id )
mat itt_acr_noweights = nullmat(itt_acr_noweights) , _b[ins_months_past_year]*100*12/18 , _se[ins_months_past_year]*100*12/18 , (_b[ins_months_past_year] + 1.96*_se[ins_months_past_year])*100*12/18, (_b[ins_months_past_year] - 1.96*_se[ins_months_past_year])*100*12/18

mat colnames itt_acr_noweights = ITT SE FirstStage SE  ACR SE Upper_CI Lower_CI

**** Column 2 

gen age_oregon_weights = 1/((1/weights_aca_penalty_notall16)*(1/weight_6_12m))


cap mat drop itt_acr_aca_weights

*ITT 20% Sample with ACA Penalty Weights
regress postn_death treatment nnn* lll* [pw = age_oregon_weights] if ins_months_past_year != ., cl(household_id)
mat itt_acr_aca_weights = nullmat(itt_acr_aca_weights) ,  _b[treatment]*100, _se[treatment]*100

*First Stage (months on any insurance from around July/August 2008 - July/August 2009 ), 20% sub-sample
reg ins_months_past_year treatment ddd*  qqq* [pw = age_oregon_weights], cl(household_id ) noomit
mat itt_acr_aca_weights = nullmat(itt_acr_aca_weights) , _b[treatment] , _se[treatment]


*ACR 20% Sample with ACA Penalty Weights with ACA Penalty Weights
ivregress 2sls postn_death (ins_months_past_year = treatment ) nnn* lll* [pw = age_oregon_weights], cl(household_id )
mat itt_acr_aca_weights = nullmat(itt_acr_aca_weights) , _b[ins_months_past_year]*100*12/18 , _se[ins_months_past_year]*100*12/18 , (_b[ins_months_past_year] + 1.96*_se[ins_months_past_year])*100*12/18, (_b[ins_months_past_year] - 1.96*_se[ins_months_past_year])*100*12/18

mat colnames itt_acr_aca_weights = ITT SE FirstStage SE ACR SE Upper_CI Lower_CI



**** Column 3 : Miller et al (2019)

local first_stage = ((0.053*12) + (0.038*12))
local itt = 100*( (-0.000888) + (-0.001190))
local acr =  `itt'/`first_stage'  

mat miller = `itt' , . , `first_stage' , . ,  `acr'
mat colnames miller = ITT SE FirstStage SE ACR 
mat li itt_acr_noweights
mat li itt_acr_aca_weights
mat li miller 

clear
mat itt_acr_noweights = [itt_acr_noweights]'
mat itt_acr_aca_weights = [itt_acr_aca_weights]'
mat miller = [miller]'

svmat itt_acr_noweights
svmat itt_acr_aca_weights
svmat miller

save "$results/Data/Prior Research Comparison - `date'.dta", replace

log close
